﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_managelink : System.Web.UI.Page
{
    Link l = new Link();

    protected void Page_Load(object sender, EventArgs e)
    {
        Function.CheckSession();
        if (!this.IsPostBack)
        {
            BindLink();
        }
    }

    void BindLink()
    {
        this.GridView1.DataSource = l.GetAllLink();
        this.GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        l.Linkname = this.TextBox1.Text;
        l.Pic = this.File1.Value;
        l.Url = this.TextBox2.Text;
        l.Type = this.RadioButtonList1.SelectedValue;
        l.AddLink(l);
        BindLink();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.File1.PostedFile != null)
        {
            string filename = File1.PostedFile.FileName;
            int i = filename.LastIndexOf(".");//获取文件名中最后一个.的索引位置
            string ex_name = filename.Substring(i);
            if (ex_name.ToUpper() != ".GIF" && ex_name.ToUpper() != ".JPG" && ex_name.ToUpper() != ".JPEG")
            {
                Response.Write("<scirpt>alert('文件格式不正确，请选择 jpg 或者 gif 格式的图片！');</script>");
                Response.End();
            }
            DateTime now = DateTime.Now;//获取当前时间
            string savefilename = now.Millisecond.ToString() + "_" + File1.PostedFile.ContentLength.ToString() + ex_name;
            File1.PostedFile.SaveAs(Server.MapPath("upload_pic/" + savefilename));
            this.Image1.ImageUrl = "upload_pic/" + savefilename;
            Response.Write("<script>alert('上传成功');</script>");
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        l.DelLink(int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString()));
        BindLink();
    }
}
